package top.superflow.security.dft

import java.security.MessageDigest

import top.superflow.security.api.Security
import top.superflow.verify.Ensure

class DefaultSecurity (customizedEncoder : String) extends Security 
                                                   with Ensure{
  
  var encoder = customizedEncoder
  
  def this(){
    this("UTF-8")
  }
  
  def encrypt (source : String, algorithm : String) : String = {
    
    notEmpty(algorithm, "algorithm should not be empty")
    notNull(source, "source should not be null")
    
    val md : MessageDigest = MessageDigest.getInstance(algorithm);
    
    md.update(source.getBytes(this.encoder)); 
    val digest  = md.digest()
    SecurityUtils.convertByteArrayToHexString(digest)
  }
  
}

object DefaultSecurity{
  
  def main(args: Array[String]): Unit = {
    val security = new DefaultSecurity
    print(security.encrypt("superflow", "SHA-256"))
  }
}